package org.example.tree;

/**
 * @author liyishan
 * @date 2024/5/16 09:59
 * @apiNote
 */

public class Test00543DiameterOfBinaryTree {
    int ans;

    public int diameterOfBinaryTree(TreeNode root) {
        ans = 1;
        depth(root);
        return ans -1;

    }

    public int depth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = depth(root.left);
        int right = depth(root.right);
        ans = Math.max(ans, left + right + 1);
        return Math.max(left,right)+1;
    }
}
